# -*- Makefile -*-
#
# Copyright 2009 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 

## ----------------------------------------------------------------
## Special rule for regenerating the local Makefile.gen
##
## Define the variable 'makefile_gen_gen_command' to be the command
## that creates $(srcdir)/Makefile.gen .  The rule below will check to
## make sure that it can create the generated file, and if not exits
## with an error -1.
##

STAMPS += $(DEPDIR)/Makefile.gen-generate-generate-*

generate-makefile $(srcdir)/Makefile.gen:
## parallel make protection; can't hurt
	@trap 'rm -rf $(DEPDIR)/Makefile.gen-generate-*' 1 2 13 15; \
	if mkdir $(DEPDIR)/Makefile.gen-generate-lock 2>/dev/null; then \
## This code is being executed by the first process.
		rm -f $(DEPDIR)/Makefile.gen-generate-stamp; \
		rm -f $(DEPDIR)/Makefile.gen-generate-tmp; \
		touch $(DEPDIR)/Makefile.gen-generate-tmp; \
## recreate $(srcdir)/Makefile.gen only if ...
		do_recreate=0; \
		if test -f $(srcdir)/Makefile.gen; then \
## the file exists and can be removed; or ...
			if $(RM) $(srcdir)/Makefile.gen 2>/dev/null; then \
				do_recreate=1; \
			fi; \
		else \
## the file doesn't exist, but can be created (e.g., by touching it).
			if touch $(srcdir)/Makefile.gen 2>/dev/null; then \
				do_recreate=1; \
			fi; \
		fi; \
		if test "$$do_recreate" == "1"; then \
			echo "Regenerating $(srcdir)/Makefile.gen"; \
			$(makefile_gen_gen_command); \
		else \
			echo "Cannot recreate $(srcdir)/Makefile.gen because the directory or file is write-protected."; \
			exit -1; \
		fi; \
		mv -f $(DEPDIR)/Makefile.gen-generate-tmp $(DEPDIR)/Makefile.gen-generate-stamp; \
		rmdir $(DEPDIR)/Makefile.gen-generate-lock; \
	else \
## This code is being executed by the follower processes.
## Wait until the first process is done.
		while test -d $(DEPDIR)/Makefile.gen-generate-lock; do sleep 1; done; \
## Succeed if and only if the first process succeeded.
		test -f $(DEPDIR)/Makefile.gen-generate-stamp; exit $$?; \
	fi;
